//
//  simViewController.m
//  simens
//
//  Created by amy zhang on 12-7-16.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "simViewController.h"
#import "simShopVisitCalendarController.h"
#import "ASIHTTPRequest.h"

@interface simViewController ()<UITextFieldDelegate,MBProgressHUDDelegate>

@property (nonatomic, strong) UITextField* userNameTextField;
@property (nonatomic, strong) UITextField* userPassWordTextField;
@end

@implementation simViewController


@synthesize userNameTextField = _userNameTextField;
@synthesize userPassWordTextField = _userPassWordTextField;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    [self.navigationController.navigationBar setHidden:YES];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"login.png"]]];
    
    //self.title=@"登录";
    UIButton* backBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBt setFrame: self.view.bounds];
    [backBt setBackgroundColor:[UIColor clearColor]];
    //定义点击时的响应函数
	[backBt addTarget:self action:@selector(missKeyBoard) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:backBt];
    
    // Button 代码参照这里写
    UIButton* loginBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBt setFrame:CGRectMake(225, 230, 49, 28)];
    //[loginBt setImage:[UIImage imageNamed:@"longinButton_up.png"] forState:UIControlStateNormal];
    //[loginBt setImage:[UIImage imageNamed:@"longinButton_down.png"] forState:UIControlStateHighlighted];
    [loginBt setBackgroundColor:[UIColor clearColor]];
    
    
    //定义点击时的响应函数
	[loginBt addTarget:self action:@selector(loginbuttonPress) forControlEvents:UIControlEventTouchUpInside]; 
    
    [self.view addSubview:loginBt];

    self.userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(86, 145, 190, 30)];
    //[self.userNameTextField setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    self.userNameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  
    [self.userNameTextField setPlaceholder:@"点击输入用户名"];
    self.userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;//编辑时，可以右边按钮全删除内容
    [self.userNameTextField setBorderStyle:(UITextBorderStyleRoundedRect)];
    [self.userNameTextField setKeyboardType:UIKeyboardTypePhonePad];
    NSString *mobile=[[NSUserDefaults standardUserDefaults]objectForKey:@"LoginMobile"];
    [self.userNameTextField setText:mobile];
    
    [self.userNameTextField setDelegate:self];
    
    //首字母小写
    [self.userNameTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    
    //[self.userNameTextField setBackgroundColor:[UIColor redColor]];
    [self.view addSubview: self.userNameTextField];
    
    self.userPassWordTextField = [[UITextField alloc] initWithFrame:CGRectMake(86, 185, 190, 30)];
    self.userPassWordTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  
    [self.userPassWordTextField setPlaceholder:@"点击输入密码"];
    self.userPassWordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;//编辑时，可以右边按钮全删除内容
    [self.userPassWordTextField setDelegate:self];
    [self.userPassWordTextField setSecureTextEntry:YES];
    //[self.userPassWordTextField setBackgroundColor:[UIColor redColor]];
    [self.userPassWordTextField setBorderStyle:(UITextBorderStyleRoundedRect)];
    [self.view addSubview: self.userPassWordTextField];
    
    
    NSString *pwd=[[NSUserDefaults standardUserDefaults]objectForKey:@"LoginPassword"];
    [self.userPassWordTextField setText:pwd];
    
    [[NSUserDefaults standardUserDefaults] setObject:@"http://61.164.42.175:8080/openApiPlatform/Service/rs/siemensService" forKey:@"WebserviceUrl"];//10.91.2.14
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(void) loginbuttonPress {
    
    if ([self.userNameTextField.text isEqualToString:@""] || self.userNameTextField.text == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"用户名为空" message:@"请输入用户名"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        
        //自动启动用户名编辑框
        [self.userNameTextField becomeFirstResponder]; 
    }
    else if ([self.userPassWordTextField.text isEqualToString:@""] || self.userPassWordTextField.text == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"密码为空" message:@"请输入密码"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        
        //自动启动密码编辑框
        [self.userPassWordTextField becomeFirstResponder]; 
    }
    else {
        [self showMbprogress];
        //登陆
        NSString* u=[NSString stringWithFormat:@"%@/siemensLogin?mobile=%@&pwd=%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"WebserviceUrl"],self.userNameTextField.text,self.userPassWordTextField.text];    
        NSURL *url = [NSURL URLWithString:u];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request startSynchronous];
        NSError *error = [request error];
        if (!error) {
            NSString *response = [request responseString];
            NSLog(@"login  is :%@",response);
            
            [self performSelector:@selector(LoginCallBack:) withObject:response afterDelay:2];//30秒后检查
            
        }
        
    }
}

-(void) LoginCallBack:(NSString*)response {
    
    [self hideMbProgress];
    
    //if ([self.userNameTextField.text isEqualToString: @"13470731979"]==YES && [self.userPassWordTextField.text isEqualToString:@"123456"]==YES) {
    if([response isEqualToString:@"true"]==YES){//登陆成功
        //保存登陆帐号密码
        [[NSUserDefaults standardUserDefaults] setObject:self.userNameTextField.text forKey:@"LoginMobile"];
        [[NSUserDefaults standardUserDefaults] setObject:self.userPassWordTextField.text forKey:@"LoginPassword"];
		[[NSUserDefaults standardUserDefaults] synchronize];
        
                simShopVisitCalendarController* shopvisitController = [[simShopVisitCalendarController alloc] init];
                
                [shopvisitController setHidesBottomBarWhenPushed:NO];
                [self.navigationController pushViewController:shopvisitController animated:YES];
        
        
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"登陆失败" message:@"密码错误或帐号不存在"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
    }
}

-(void) missKeyBoard {
    
    [self.userNameTextField resignFirstResponder]; 
    [self.userPassWordTextField resignFirstResponder]; 
}


//iPad键盘的Retuan...电脑的回车键
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if (textField == self.userNameTextField) {
        
        //用户名输入完成，点击键盘Return
        //自动弹出密码输入
        [self.userPassWordTextField becomeFirstResponder];
        
    }else if (textField == self.userPassWordTextField) {
        //启动登陆
        [self loginbuttonPress];
    }
    return YES;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end
